package org.wkg;

import org.wkg.utils.DBUtil;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo05_Insert {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement statement = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "insert into bank_account (name, balance) values (?,?)";
            statement = connection.prepareStatement(sql);
            // 4. 接收用户输入
            System.out.println("请输入姓名：");
            Scanner scanner = new Scanner(System.in);
            String name = scanner.next();
            System.out.println("请输入余额：");
            BigDecimal balance = new BigDecimal(scanner.next());
            statement.setString(1, name);
            statement.setBigDecimal(2, balance);
            int row = statement.executeUpdate();
            if (row == 1) {
                System.out.println("插入成功!!!");
            } else {
                System.out.println("插入失败!!!");
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(null, statement, connection);
        }
    }


}
